<?php

namespace app\serverbankcard\logic;
use \app\manage\logic\BaseLogic;

/**
 * @author:      李颖
 * @DateTime:    2017-08-30 10:26:10
 * @Description: 银行卡管理
 */

class Bankcard extends BaseLogic{
	
	protected $name = 'bankcard';
    protected $config = 'db_config_bankcard';
    protected $bankcardModel = '\app\serverbankcard\model\Bankcard';

    /*我的 - 添加银行卡*/
    public function addCard($data): array 
    {
        //若该银行卡存在
        $_where['number'] = $data['number'];
        $cardInfo = $this->model($this->bankcardModel)->where($_where)->find();
        if($cardInfo && $cardInfo['status'] != '-1' ){
            return verify_err(null, '该银行卡号已存在');
        }
    	$data['hide_number'] = substr($data['number'], -4);
        try{
            $res = $this->model($this->bankcardModel)->save($data);
        }catch(\Exception $e){
            return server_err(null, $e->getMessage());
        }
    	
    	return auto_return($res, '成功添加银行卡', '添加银行卡失败');
    }

    /*我的 - 银行卡列表*/
    public function userCardList($data): array 
    {
    	$_where['user_id'] = $data['user_id'];

    	if(isset($data['status']) && $data['status'] !== ''){ 
    		$_where['status'] = $data['status']; 
    	}else{
    		$_where['status'] = ['neq',-1];
    	}

    	$list = $this->model($this->bankcardModel)->where($_where)->select();
    	return auto_return($list);
    }

    /*后台 - 获取银行卡列表*/
    public function cardList($data): array
    {
        if(isset($data['keywords']) && $data['keywords']){ $_where['user_id|account|name'] = ['like', '%' . $data['keywords'] . '%']; }
        if(isset($data['status']) && $data['status'] !== ''){
            $_where['status'] = $data['status'];
        }else{
            $_where['status'] = ['neq',-1];
        }
    	$res = $this->model($this->bankcardModel)
                    ->where($_where)
                    ->paginate($data['listRows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res);
    }

    /*我的/管理员 - 删除银行卡*/
    public function delCard($data): array 
    {
    	$_where['id'] = $data['id'];
    	isset($data['user_id']) && $_where['user_id'] = $data['user_id'];
    	$_where['status'] = ['neq', -1];
    	$res = $this->model($this->bankcardModel)->save(['status'=>-1], $_where);
    	return auto_return($res, '成功删除银行卡', '删除银行卡失败');
    }

    /*后台 - 审核银行卡*/
    public function audit($data): array 
    {
    	$_where['id'] = $data['id'];
    	$_where['status'] = 0;
    	$saveData['status'] = $data['status'];
    	$res = $this->model($this->bankcardModel)->save($saveData, $_where);
    	return auto_return($res);
    }

    /*获取我的某一张银行卡*/
    public function userCardInfo($data): array 
    {
        $_where['id'] = $data['id'];
        $_where['user_id'] = $data['user_id'];
        $_where['status'] = $data['status'] !== '' ? $data['status'] : ['neq',-1];
        $res = $this->model($this->bankcardModel)->where($_where)->find();
        return auto_return($res, '成功获取银行卡信息', '获取银行卡信息失败');
    }
}